package com.example.demo.controller;

import com.example.demo.common.domain.CompensatoryLeaveRecord;
import com.example.demo.common.domain.Employee;
import com.example.demo.common.util.Result;
import com.example.demo.common.util.StringUtil;
import com.example.demo.service.ComLeaveService;
import com.example.demo.service.DateService;
import com.example.demo.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class ComLeaveController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DateService dateService;

    @Autowired
    private ComLeaveService comLeaveService;
    public boolean check(HttpSession session){//检查是否登录
        if(session.getAttribute("employee")!=null)
            return true;
        else
            return false;
    }
    @RequestMapping("/subComLeave")
    @ResponseBody
    public Map subComLeave(HttpSession session, Model model,String startDate, String startTime,
                           String endDate, String endTime, String reason,String nowDate,
                           String nowTime){//提交一份调休申请
        Employee e = (Employee)session.getAttribute("employee");

        startDate = StringUtil.rmQuote(startDate);
        startTime = StringUtil.rmQuote(startTime);
        endDate = StringUtil.rmQuote(endDate);
        endTime = StringUtil.rmQuote(endTime);
        reason = StringUtil.rmQuote(reason);

        nowDate = StringUtil.rmQuote(nowDate);
        nowTime = StringUtil.rmQuote(nowTime);

        Date start = dateService.parseDate(startDate,startTime);
        Date end = dateService.parseDate(endDate,endTime);
        Date now = dateService.parseDate(nowDate,nowTime);

        //Date dur = dateService.sub(end,start);
        //System.out.println(dur.toString());
        CompensatoryLeaveRecord compensatoryLeaveRecord = new CompensatoryLeaveRecord();

        compensatoryLeaveRecord.setCsrc(start);
        compensatoryLeaveRecord.setCdst(now);
        compensatoryLeaveRecord.setDuration(end);
        compensatoryLeaveRecord.setCdescription(reason);
        compensatoryLeaveRecord.setEid(e.getEid());
        System.out.println(compensatoryLeaveRecord);
        comLeaveService.insertSelective(compensatoryLeaveRecord);

        return Result.success(true);
    }
    @RequestMapping("/comleave")
    public String comleave(HttpSession session){
        if(!check(session))
            return "login";
        return "comleave";
    }

    @RequestMapping("/comleave2")
    public String comleave2public(HttpSession session,Model model,
                                                @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                                                @RequestParam(required = false,defaultValue = "6") Integer pageSize
    ){//搜索一定范围的调休申请，返回到调休列表中
        PageHelper.startPage(pageNo,pageSize);
        if(!check(session)){
            return "login";
        }
        List<CompensatoryLeaveRecord> compensatoryLeaveRecords = comLeaveService.selectAll();

        PageInfo<CompensatoryLeaveRecord> pageInfo = new PageInfo(compensatoryLeaveRecords);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo",pageInfo);

        return "comleave2";
    }

    @RequestMapping("/comleave3")
    public String leave3(HttpSession session,Model model,Integer cid){//显示某个调休申请的详情
        if(!check(session)){
            return  "login";
        }
        CompensatoryLeaveRecord compensatoryLeaveRecord = comLeaveService.selectByPrimaryId(cid);
        Date end = compensatoryLeaveRecord.getDuration();
        Date start = compensatoryLeaveRecord.getCsrc();

        Date dur = dateService.sub(end,start);
        String format = dateService.DateDur2Str(dur);

        model.addAttribute("clrecord",compensatoryLeaveRecord);
        String percent = "0";
        if(compensatoryLeaveRecord.getCgeneralagree() == 1){
            percent = "100";
        }else if(compensatoryLeaveRecord.getCdeputyagree() == 1){
            percent = "66";
        }else{
            percent = "33";
        }

        model.addAttribute("percent",percent);
        Employee e = employeeService.selectByPrimaryKey(compensatoryLeaveRecord.getEid());
        model.addAttribute("employee",e);
        model.addAttribute("format",format);
        System.out.println(compensatoryLeaveRecord);
        return "comleave3";
    }

}
